package com.rtx.edi.platform.config.controller;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.api.enums.FieldValidateType;
import com.rtx.api.utils.FieldValidateUtils;
import com.rtx.edi.model.api.dto.PlApiMappingConfigDTO;
import com.rtx.edi.model.api.po.PlApiMappingConfig;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.config.service.PlApiMappingConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 对接场景信息表 前端控制器
 * </p>
 *
 * @author cafebabe
 * @since 2023-03-10
 */
@Api(tags = {"对接场景sql配置"})
@RestController
@RequestMapping("/v1/edi/PlApiMappingConfig")
public class PlApiMappingConfigController {
	
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private PlApiMappingConfigService plApiMappingConfigService; 
	
	
	@PostMapping("executeSqlByMappingId")
	public EdiResponse executeSqlByMappingId(@RequestBody String requestStr,@RequestParam("mappingId")Long mappingId) {
		return EdiResponse.success().data("item", plApiMappingConfigService.executeSqlByMappingId(mappingId,requestStr));
	}

	/**
	 * 	根据配置场景id获取 对接场景sql配置明细
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "根据配置场景id获取 对接场景sql配置明细", notes = "根据配置场景id获取 对接场景sql配置明细")
	@PostMapping("selectPlApiMappingConfigListByMappingId")
	public EdiResponse selectConfigByMappingId(@RequestBody PlApiMappingConfigDTO dto) {
		return EdiResponse.success().data("item", plApiMappingConfigService.queryByMappingId(dto));
	}
	
	/**
	 * 获取对接场景列表
	 * @return
	 */
	@ApiOperation(value = "对接场景配置列表_分页", notes = "对接场景配置列表_分页")
	@PostMapping("selectPlApiMappingConfigList")
	public EdiResponse selectPlApiMappingList(@RequestBody(required = false) PlApiMappingConfigDTO dto){
		PlApiMappingConfig po = new PlApiMappingConfig();
		BeanUtils.copyProperties(dto, po);
		Page<PlApiMappingConfig> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
		plApiMappingConfigService.selectPlApiMappingConfigList(pageParam,po);
		return EdiResponse.success().data("page",pageParam);
	}
	

	/**
	 * 根据ID获取对接场景
	 * @param PlApiMapping
	 * @return
	 */
	@PostMapping("selectPlApiMappingConfigById")
	public EdiResponse selectPlApiMappingById(@RequestBody PlApiMappingConfigDTO dto){
		QueryWrapper<PlApiMappingConfig> qw = new QueryWrapper<PlApiMappingConfig>();
		qw.eq("id", dto.getId());
		return EdiResponse.success().data("item",Optional.ofNullable(plApiMappingConfigService.getOne(qw)).orElse(new PlApiMappingConfig()));
	}
	/**
	 *  	根据id删除
	 * @param PlApiMapping
	 * @return
	 */
	@ApiOperation(value = "删除对接场景配置", notes = "根据id删除对接场景配置", httpMethod = "POST")
	@PostMapping("deletePlApiMappingConfigId")
	public EdiResponse deletePlApiMappingById(@RequestBody PlApiMappingConfigDTO dto) {
		return plApiMappingConfigService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
	}
	
    /**
     * 根据id列表删除
     * @param list
     * @return
     */
    @ApiOperation(value = "删除对接场景_批量", notes = "根据id列表删除对接场景", httpMethod = "POST")
    @PostMapping("deletePlApiMappingConfigByIds")
    public EdiResponse deletePlApplicationByIds(@RequestBody List<PlApiMappingConfigDTO> list) {
    	List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
    	return  plApiMappingConfigService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }

	/**
	 * 新增对接场景
	 * @param PlApiMapping
	 * @return
	 */
	@ApiOperation(value = "新增对接场景配置")
	@PostMapping("addPlApiMappingConfig")
	public EdiResponse addPlApiMapping(@RequestBody PlApiMappingConfigDTO dto){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		String companyid =  (String) Optional.ofNullable(session.getAttribute("companyid")).orElse("1");
		
		dto.setCompanyId(Long.parseLong(companyid));
		
        FieldValidateUtils.fieldNotNull(dto, FieldValidateType.INSERT);
        PlApiMappingConfig po = new PlApiMappingConfig();
        BeanUtils.copyProperties(dto,po);
//        po.setQueryColumn(JSON.toJSONString(dto.getQueryColumn()));
		plApiMappingConfigService.save(po);
		return EdiResponse.success();
	}

	/**
	 * 根据ID修改对接场景
	 * @return
	 */
	@ApiOperation(value = "修改对接场景配置", notes = "根据ID修改对接场景")
	@PostMapping("editPlApiMappingById")
	public EdiResponse editPlApiMappingById(@RequestBody PlApiMappingConfigDTO dto){
		PlApiMappingConfig po = new PlApiMappingConfig();
		BeanUtils.copyProperties(dto, po);
		return plApiMappingConfigService.updateById(po) ? EdiResponse.success() : EdiResponse.failed();
	}
}
